package cn.edu.aynu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.pool.DruidDataSource;

import cn.edu.aynu.pojo.BaseDict;
import cn.edu.aynu.pojo.Customer;
import cn.edu.aynu.pojo.QueryVo;
import cn.edu.aynu.service.BaseDictService;
import cn.edu.aynu.service.CustomerService;
import cn.edu.aynu.utils.Page;

/**
 * 客户管理
 *
 * @author lx
 */
@Controller
public class CustomerController
{
    @Autowired
    private BaseDictService baseDictService;
    
    @Autowired
    private CustomerService customerService;
    
    // 注解在成员变量上
    @Value("${fromType_code}")
    private String fromTypeCode;
    
    @Value("${industryType_code}")
    private String industryTypeCode;
    
    @Value("${levelType_code}")
    private String levelTypeCode;
    
    @Autowired
    DruidDataSource dataSource;
    
    // 入口
    @RequestMapping(value = {"/", "/customer/list"})
    public String list(QueryVo vo, Model model)
    {
        List<BaseDict> fromType = baseDictService.selectBaseDictListByCode(fromTypeCode);
        List<BaseDict> industryType = baseDictService.selectBaseDictListByCode(industryTypeCode);
        List<BaseDict> levelType = baseDictService.selectBaseDictListByCode(levelTypeCode);
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);
        model.addAttribute("jdbcUrl", dataSource.getRawJdbcUrl());
        
        // 通过四个条件 查询分页对象
        Page<Customer> page = customerService.selectPageByQueryVo(vo);
        model.addAttribute("page", page);
        model.addAttribute("custName", vo.getCustName());
        model.addAttribute("custSource", vo.getCustSource());
        model.addAttribute("custIndustry", vo.getCustIndustry());
        model.addAttribute("custLevel", vo.getCustLevel());
        return "customer";
    }
    
    // 去修改页面
    @RequestMapping(value = "/customer/edit.action")
    @ResponseBody
    public Customer edit(Integer id)
    {
        return customerService.selectCustomerById(id);
    }
    
    // 修改保存
    @RequestMapping(value = "/customer/update.action")
    @ResponseBody
    public String update(Customer customer)
    {
        // 修改
        customerService.updateCustomerById(customer);
        return "OK";
    }
    
    // 修改保存
    @RequestMapping(value = "customer/create.action")
    @ResponseBody
    public String create(Customer customer)
    {
        customerService.createCustomer(customer);
        return "OK";
    }
    
    // 删除
    @RequestMapping(value = "/customer/delete.action")
    @ResponseBody
    public String delete(Integer id)
    {
        customerService.deleteCustomerById(id);
        return "OK";
    }
    
}
